<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="1：出借人 2：借款人" prop="userType">
      <el-input v-model="dataForm.userType" placeholder="1：出借人 2：借款人"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="用户密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="用户密码"></el-input>
    </el-form-item>
    <el-form-item label="用户昵称" prop="nickName">
      <el-input v-model="dataForm.nickName" placeholder="用户昵称"></el-input>
    </el-form-item>
    <el-form-item label="用户姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="用户姓名"></el-input>
    </el-form-item>
    <el-form-item label="身份证号" prop="idCard">
      <el-input v-model="dataForm.idCard" placeholder="身份证号"></el-input>
    </el-form-item>
    <el-form-item label="邮箱" prop="email">
      <el-input v-model="dataForm.email" placeholder="邮箱"></el-input>
    </el-form-item>
    <el-form-item label="微信用户标识openid" prop="openid">
      <el-input v-model="dataForm.openid" placeholder="微信用户标识openid"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="headImg">
      <el-input v-model="dataForm.headImg" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="绑定状态（0：未绑定，1：绑定成功 -1：绑定失败）" prop="bindStatus">
      <el-input v-model="dataForm.bindStatus" placeholder="绑定状态（0：未绑定，1：绑定成功 -1：绑定失败）"></el-input>
    </el-form-item>
    <el-form-item label="借款人认证状态（0：未认证 1：认证中 2：认证通过 -1：认证失败）" prop="borrowAuthStatus">
      <el-input v-model="dataForm.borrowAuthStatus" placeholder="借款人认证状态（0：未认证 1：认证中 2：认证通过 -1：认证失败）"></el-input>
    </el-form-item>
    <el-form-item label="绑定账户协议号" prop="bindCode">
      <el-input v-model="dataForm.bindCode" placeholder="绑定账户协议号"></el-input>
    </el-form-item>
    <el-form-item label="用户积分" prop="integral">
      <el-input v-model="dataForm.integral" placeholder="用户积分"></el-input>
    </el-form-item>
    <el-form-item label="状态（0：锁定 1：正常）" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态（0：锁定 1：正常）"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="逻辑删除(1:已删除，0:未删除)" prop="isDeleted">
      <el-input v-model="dataForm.isDeleted" placeholder="逻辑删除(1:已删除，0:未删除)"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          userType: '',
          mobile: '',
          password: '',
          nickName: '',
          name: '',
          idCard: '',
          email: '',
          openid: '',
          headImg: '',
          bindStatus: '',
          borrowAuthStatus: '',
          bindCode: '',
          integral: '',
          status: '',
          createTime: '',
          updateTime: '',
          isDeleted: ''
        },
        dataRule: {
          userType: [
            { required: true, message: '1：出借人 2：借款人不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '用户密码不能为空', trigger: 'blur' }
          ],
          nickName: [
            { required: true, message: '用户昵称不能为空', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '用户姓名不能为空', trigger: 'blur' }
          ],
          idCard: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' }
          ],
          email: [
            { required: true, message: '邮箱不能为空', trigger: 'blur' }
          ],
          openid: [
            { required: true, message: '微信用户标识openid不能为空', trigger: 'blur' }
          ],
          headImg: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          bindStatus: [
            { required: true, message: '绑定状态（0：未绑定，1：绑定成功 -1：绑定失败）不能为空', trigger: 'blur' }
          ],
          borrowAuthStatus: [
            { required: true, message: '借款人认证状态（0：未认证 1：认证中 2：认证通过 -1：认证失败）不能为空', trigger: 'blur' }
          ],
          bindCode: [
            { required: true, message: '绑定账户协议号不能为空', trigger: 'blur' }
          ],
          integral: [
            { required: true, message: '用户积分不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态（0：锁定 1：正常）不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          isDeleted: [
            { required: true, message: '逻辑删除(1:已删除，0:未删除)不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/core/userinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userType = data.userInfo.userType
                this.dataForm.mobile = data.userInfo.mobile
                this.dataForm.password = data.userInfo.password
                this.dataForm.nickName = data.userInfo.nickName
                this.dataForm.name = data.userInfo.name
                this.dataForm.idCard = data.userInfo.idCard
                this.dataForm.email = data.userInfo.email
                this.dataForm.openid = data.userInfo.openid
                this.dataForm.headImg = data.userInfo.headImg
                this.dataForm.bindStatus = data.userInfo.bindStatus
                this.dataForm.borrowAuthStatus = data.userInfo.borrowAuthStatus
                this.dataForm.bindCode = data.userInfo.bindCode
                this.dataForm.integral = data.userInfo.integral
                this.dataForm.status = data.userInfo.status
                this.dataForm.createTime = data.userInfo.createTime
                this.dataForm.updateTime = data.userInfo.updateTime
                this.dataForm.isDeleted = data.userInfo.isDeleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/core/userinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userType': this.dataForm.userType,
                'mobile': this.dataForm.mobile,
                'password': this.dataForm.password,
                'nickName': this.dataForm.nickName,
                'name': this.dataForm.name,
                'idCard': this.dataForm.idCard,
                'email': this.dataForm.email,
                'openid': this.dataForm.openid,
                'headImg': this.dataForm.headImg,
                'bindStatus': this.dataForm.bindStatus,
                'borrowAuthStatus': this.dataForm.borrowAuthStatus,
                'bindCode': this.dataForm.bindCode,
                'integral': this.dataForm.integral,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'isDeleted': this.dataForm.isDeleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
